import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
#from graspologic.plot import heatmap
from grapgh_io import GraphIO
ciona, ciona_e_att, ciona_n_att, ciona_g_att = GraphIO.load('ciona.json')
fafb, fafb_e_att, fafb_n_att, fafb_g_att = GraphIO.load('fafb.json')
worm, worm_e_att, worm_n_att, worm_g_att = GraphIO.load('worm_wiring_connectome_0_SL.json')
fafb_sides = []
def homophilic_att(graph, name):
if name == 'ciona':
for key in graph.edges:
try:
if graph.nodes[key[0]]['Side'] == graph.nodes[key[1]]['Side']:
graph.edges[key]['Homophilic'] = True
else:
graph.edges[key]['Homophilic'] = False
except:
continue
elif name == 'worm':
for key in graph.edges:
try:
if graph.nodes[key[0]]['attr_dict']['hemisphere'] == graph.nodes[key[1]]['attr_dict']['hemisphere']:
graph.edges[key]['Homophilic'] = True
else:
graph.edges[key]['Homophilic'] = False
except:
continue
elif name == 'fafb':
for key in graph.edges:
if graph.nodes[key[0]]['annotation'][0][-2] == 'L':
graph.nodes[key[0]]['Side'] = 'L'
if graph.nodes[key[0]]['annotation'][0][-2] == graph.nodes[key[1]]['annotation'][0][-2]:
graph.edges[key]['Homophilic'] = True
graph.nodes[key[1]]['Side'] = 'L'
fafb_sides.append(key[0])
fafb_sides.append(key[1])
elif graph.nodes[key[0]]['annotation'][0][-2] == 'R':
graph.nodes[key[0]]['Side'] = 'R'
graph.edges[key]['Homophilic'] = False
fafb_sides.append(key[0])
fafb_sides.append(key[1])
else:
graph.edges[key]['Homophilic'] = False
elif graph.nodes[key[0]]['annotation'][0][-2] == 'R':
graph.nodes[key[0]]['Side'] = 'R'
if graph.nodes[key[0]]['annotation'][0][-2] == graph.nodes[key[1]]['annotation'][0][-2]:
graph.edges[key]['Homophilic'] = True
graph.nodes[key[1]]['Side'] = 'R'
fafb_sides.append(key[0])
fafb_sides.append(key[1])
elif graph.nodes[key[0]]['annotation'][0][-2] == 'L':
graph.nodes[key[0]]['Side'] = 'L'
graph.edges[key]['Homophilic'] = False
fafb_sides.append(key[0])
fafb_sides.append(key[1])
else:
graph.edges[key]['Homophilic'] = False
else:
graph.edges[key]['Homophilic'] = False
def homotopic_att(graph, name):
if name == 'ciona':
for key in graph.edges:
try:
# Originally with cell type, but not enough samples
if graph.nodes[key[0]]['Brain Region'] == graph.nodes[key[1]]['Brain Region'] and graph.edges[key]['Homophilic'] == False:
graph.edges[key]['Homotopic'] = True
else:
graph.edges[key]['Homotopic'] = False
except:
continue
elif name == 'worm':
for key in graph.edges:
try:
if (graph.nodes[key[0]]['attr_dict']['ID'][:-1] == graph.nodes[key[1]]['attr_dict']['ID'][:-1]) and (graph.nodes[key[0]]['attr_dict']['ID'][-1] != graph.nodes[key[1]]['attr_dict']['ID'][-1]) and graph.edges[key]['Homophilic'] == False:
graph.edges[key]['Homotopic'] = True
else:
graph.edges[key]['Homotopic'] = False
except:
continue
elif name == 'fafb':
for key in graph.edges:
try:
if (graph.nodes[key[0]]['annotation'][0][:-2] == graph.nodes[key[1]]['annotation'][0][:-2]) and (graph.nodes[key[0]]['annotation'][0][-1] == graph.nodes[key[1]]['annotation'][0][-1]) and graph.edges[key]['Homophilic'] == False:
graph.edges[key]['Homotopic'] = True
else:
graph.edges[key]['Homotopic'] = False
except:
continue
homophilic_att(ciona, 'ciona')
homotopic_att(ciona, 'ciona')
homophilic_att(worm, 'worm')
homotopic_att(worm, 'worm')
homophilic_att(fafb, 'fafb')
homotopic_att(fafb, 'fafb')
# if (worm.nodes[key[0]]['attr_dict']['ID'][-1] == 'L' or worm.nodes[key[0]]['attr_dict']['ID'][-1] == 'R') and (worm.nodes[key[1]]['attr_dict']['ID'][-1] == 'L' or worm.nodes[key[1]]['attr_dict']['ID'][-1] == 'R')
# graph.nodes[key[1]]['annotation'][0][:-1]) and (graph.nodes[key[0]]['attr_dict']['ID'][-1] != graph.nodes[key[1]]['attr_dict']['ID'][-1]) and graph.edges[key]['Homophilic'] == False:
worm.edges[(18, 19, 0)]
worm.nodes[19]
worm.nodes[18]
import random
def find_sides(graph, name):
affinity_nodes = []
positions = {}
y_pos = 1
x_left_pos = -20
x_right_pos = 20
for key in graph.nodes:
try:
if name == 'ciona' or 'fafb':
if graph.nodes[key]['Side'] == 'L' :
affinity_nodes.append(key)
positions[key] = np.asarray([x_left_pos + random.uniform(-5, 5), y_pos])
elif graph.nodes[key]['Side'] == 'R':
affinity_nodes.append(key)
positions[key] = np.asarray([x_right_pos + random.uniform(-5, 5), y_pos])
elif name == 'worm':
if graph.nodes[key]['attr_dict']['hemisphere'] == 'left' :
affinity_nodes.append(key)
positions[key] = np.asarray([x_left_pos + random.uniform(-5, 5), y_pos])
elif graph.nodes[key]['attr_dict']['hemisphere'] == 'right' :
affinity_nodes.append(key)
positions[key] = np.asarray([x_right_pos + random.uniform(-5, 5), y_pos])
y_pos += 1
except:
y_pos += 1
continue
return affinity_nodes, positions
ciona_sides, ciona_coors = find_sides(ciona, 'ciona')
worm_sides, worm_coors = find_sides(worm, 'worm')
fafb_sides, fafb_coors = find_sides(fafb, 'fafb')
#Ciona: Homophilic in White, Non-Homophilic in Black
#Left hemisphere on the left, right on the right
edge_colors = []
for line in nx.generate_edgelist(ciona.subgraph(ciona_sides), data=False):
edge = []
edge.append(line.rsplit(' ', 1)[0])
edge.append(line.rsplit(' ', 1)[1])
edge = tuple(edge)
if ciona.subgraph(ciona_sides).edges[edge]['Homophilic']:
edge_colors.append("white")
else:
edge_colors.append("black")
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(ciona.subgraph(ciona_sides), pos=ciona_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
#Ciona: Non-Homotopic in White, Homotopic in Black
#Left hemisphere on the left, right on the right
edge_colors = []
for line in nx.generate_edgelist(ciona.subgraph(ciona_sides), data=False):
edge = []
edge.append(line.rsplit(' ', 1)[0])
edge.append(line.rsplit(' ', 1)[1])
edge = tuple(edge)
if ciona.subgraph(ciona_sides).edges[edge]['Homotopic']:
edge_colors.append("black")
else:
edge_colors.append("white")
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(ciona.subgraph(ciona_sides), pos=ciona_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
#Worm: Homophilic in White, Non-Homophilic in Black
#Left hemisphere on the left, right on the right
edge_colors = []
for edge in worm.subgraph(worm_sides).edges:
if worm.subgraph(worm_sides).edges[edge]['Homophilic']:
edge_colors.append("white")
else:
edge_colors.append("black")
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(worm.subgraph(worm_sides), pos=worm_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
#Worm: Non-Homotopic in White, Homotopic in Black
#Left hemisphere on the left, right on the right
edge_colors = []
for edge in worm.subgraph(worm_sides).edges:
if worm.subgraph(worm_sides).edges[edge]['Homotopic']:
edge_colors.append("black")
else:
edge_colors.append("white")
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(worm.subgraph(worm_sides), pos=worm_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
#Fafb: Homophilic in White, Non-Homophilic in Black
#Left hemisphere on the left, right on the right
edge_colors = []
for line in nx.generate_edgelist(fafb.subgraph(fafb_sides), data=False):
edge = []
edge.append(int(line.rsplit(' ', 1)[0]))
edge.append(int(line.rsplit(' ', 1)[1]))
edge = tuple(edge)
if fafb.subgraph(fafb_sides).edges[edge]['Homophilic']:
edge_colors.append("white")
else:
edge_colors.append("black")
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(fafb.subgraph(fafb_sides), pos=fafb_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
#Fafb: Non-Homotopic in White, Homotopic in Black
#Left hemisphere on the left, right on the right
edge_colors = []
for line in nx.generate_edgelist(fafb.subgraph(fafb_sides), data=False):
edge = []
edge.append(int(line.rsplit(' ', 1)[0]))
edge.append(int(line.rsplit(' ', 1)[1]))
edge = tuple(edge)
if fafb.subgraph(fafb_sides).edges[edge]['Homotopic']:
edge_colors.append("black")
else:
edge_colors.append("white")
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(fafb.subgraph(fafb_sides), pos=fafb_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
!jupyter nbconvert --to html Affinity_Testing.ipynb